package com.example.controller.admin;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.common.R;
import com.example.mapper.AnnouceMapper;
import com.example.mapper.ServiceMapper;
import com.example.model.req.AddServiceReq;
import com.example.model.resp.ListAdminServiceResp;
import com.example.model.resp.ListCategoryResp;
import com.example.model.resp.ListDetailServiceResp;
import com.example.service.CategoryService;
import com.example.service.ServiceService;
import com.example.utils.ImageUtil;

@ResponseBody
@RestController
@RequestMapping("/admin")
public class AdminServiceController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ServiceMapper serviceMapper;
    @Autowired
    private ServiceService serviceService;
    @Autowired
    private AnnouceMapper annouceMapper;
    @Autowired
    private ImageUtil imageUtil;

    @GetMapping("/service/list")
    public R<List<ListAdminServiceResp>> listAdminServices() {
        return R.success(serviceMapper.listAdminServices());
    }

    @GetMapping("/totalServices")
    public R<Integer> totalEmployee() {
        return R.success(serviceMapper.totalService());
    }

    @GetMapping("/service/listDetail")
    public R<List<ListDetailServiceResp>> listDtailService(@RequestParam("pageSize") Integer pageSize,
            @RequestParam("currentPage") Integer currentPage) {
        return R.success(serviceMapper.listDetailServices((currentPage - 1) * pageSize, pageSize));
    }

    @GetMapping("/getAnnounce")
    public R<String> getAnnouce() {
        return R.success(annouceMapper.getAnnounce());
    }

    @PostMapping("/updateAnnounce")
    public R<String> updateAnnounce(@RequestParam("announce") String announce) {
        annouceMapper.updateAnnounce(announce);
        return R.success();
    }

    @PostMapping("/upload")
    public String uploadAvatar(@RequestParam("file") MultipartFile filePath)
            throws IOException {
        String fileName = filePath.getOriginalFilename();
        String imgName = imageUtil.getRandomImgName(fileName);
        if (!filePath.isEmpty()) {
            FileInputStream inputStream = (FileInputStream) filePath.getInputStream();
            String path = imageUtil.uploadImageQiniu(inputStream, imgName);
            return path;
        }
        return "";
    }

    @GetMapping("/getAllCategory")
    public R<List<ListCategoryResp>> getAllCategory() {
        return categoryService.getCategoryList();
    }

    @PostMapping("/add")
    public R<String> addNewService(@RequestBody() AddServiceReq addServiceReq) {
        serviceService.addService(addServiceReq);
        return R.success();
    }

    @PostMapping("/deleteService/{serviceId}")
    public R<String> deleteService(@PathVariable("serviceId") Integer serviceId) {
        serviceService.deleteService(serviceId);
        return R.success();
    }
}
